package com.youyun56.gps.android.service;

import java.util.List;

import org.json.JSONObject;

import android.os.Message;
import android.util.Log;

import com.youyun56.gps.android.CarStatusActivity;
import com.youyun56.gps.android.GoodInfoActivity;
import com.youyun56.gps.android.GpsActivity;
import com.youyun56.gps.android.ImsActivity;
import com.youyun56.gps.android.ImsHandler;
import com.youyun56.gps.android.UserInfoActivity;
import com.youyun56.gps.android.commands.CommandType;
import com.youyun56.gps.android.dao.ImDao;
import com.youyun56.gps.android.pojo.Im;

public class ClientSessionHandler {
    private LocationUploadService service;
    private final static String   tag = ClientSessionHandler.class.getName();
    
    public ClientSessionHandler(LocationUploadService service) {
        this.service = service;
    }
    
    private void assignMessage(String message) {
        Log.i("RECEIVED: ", message);
        try {
            
            JSONObject input = new JSONObject(message);
            if (input != null) {
                if (!input.has("id")) {
                    return;
                }
                int what = input.getInt("id");
                switch (what) {
                    case CommandType.Login:
                        Message loginMsg = GpsActivity.handler.obtainMessage();
                        loginMsg.obj = input;
                        loginMsg.what = what;
                        loginMsg.sendToTarget();
                        break;
                    case CommandType.GetGoods:
                        Message getGoodMsg = GoodInfoActivity.handler.obtainMessage();
                        getGoodMsg.obj = input;
                        getGoodMsg.what = what;
                        getGoodMsg.sendToTarget();
                        break;
                    case CommandType.Append_Good_END:
                        Message appendGoodEndMsg = GoodInfoActivity.handler.obtainMessage();
                        appendGoodEndMsg.obj = input;
                        appendGoodEndMsg.what = what;
                        appendGoodEndMsg.sendToTarget();
                        break;
                    case CommandType.Good_Search:
                        Message searchGoodMsg = GoodInfoActivity.handler.obtainMessage();
                        searchGoodMsg.obj = input;
                        searchGoodMsg.what = what;
                        searchGoodMsg.sendToTarget();
                        break;
                    case CommandType.Good_Search_END:
                        Message searchGoodEndMsg = GoodInfoActivity.handler.obtainMessage();
                        searchGoodEndMsg.obj = input;
                        searchGoodEndMsg.what = what;
                        searchGoodEndMsg.sendToTarget();
                        break;
                    case CommandType.ADD_IM:
                        if (ImsActivity.handler != null) {
                            Message addImMsg = ImsActivity.handler.obtainMessage();
                            addImMsg.obj = input;
                            addImMsg.what = what;
                            addImMsg.sendToTarget();
                        } else {
                            List<Im> ims = ImsHandler.createIms(input);
                            for (Im im : ims) {
                                ImDao.getInstance().saveIm(im);
                            }
                        }
                        break;
                    case CommandType.Change_Car_Hearvy:
                        Message carryStatus = CarStatusActivity.handler.obtainMessage();
                        carryStatus.obj = input;
                        carryStatus.what = what;
                        carryStatus.sendToTarget();
                        break;
                    case CommandType.Update_Car_Status:
                        Message carInfo = UserInfoActivity.handler.obtainMessage();
                        carInfo.obj = input;
                        carInfo.what = what;
                        carInfo.sendToTarget();
                        break;
                    case CommandType.AddGood:
                        while (GoodInfoActivity.handler == null) {
                            try {
                                Thread.sleep(200);
                            } catch (Exception e) {
                                // TODO: handle exception
                            }
                        }
                        Message goodMsg = GoodInfoActivity.handler.obtainMessage();
                        goodMsg.obj = input;
                        goodMsg.what = what;
                        goodMsg.sendToTarget();
                        break;
                    case CommandType.Re_Connect:
                        Message reconnectGoods = GoodInfoActivity.handler.obtainMessage();
                        reconnectGoods.obj = input;
                        reconnectGoods.what = what;
                        reconnectGoods.sendToTarget();
                        break;
                    default:
                        Log.e(tag, String.format("%s is not invalid command", input));
                }
            }
            
        } catch (Exception e) {
            Log.e(tag, e.getMessage(), e);
        }
        
    }
    
}
